package sdk.io.grpc.helloworld;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import sdk.io.grpc.helloworld.proto.HelloGrpc;
import sdk.io.grpc.helloworld.proto.HelloRequest;
import sdk.io.grpc.helloworld.proto.HelloResponse;


public class CompressingHelloWorldClient {
    private static final Logger logger =
            Logger.getLogger(CompressingHelloWorldClient.class.getName());

        private final ManagedChannel channel;
        private final HelloGrpc.HelloBlockingStub blockingStub;

        /** Construct client connecting to HelloWorld server at {@code host:port}. */
        public CompressingHelloWorldClient(String host, int port) {
          channel = ManagedChannelBuilder.forAddress(host, port)
              .usePlaintext()
              .build();
          blockingStub = HelloGrpc.newBlockingStub(channel);
        }

        public void shutdown() throws InterruptedException {
          channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
        }

        /** Say hello to server. */
        public void greet(String name) {
          logger.info("Will try to greet " + name + " ...");
          HelloRequest request = HelloRequest.newBuilder().setName(name).build();
          HelloResponse response;
          try {
            // This enables compression for requests. Independent of this setting, servers choose whether
            // to compress responses.
            response = blockingStub.withCompression("gzip").call(request);
          } catch (StatusRuntimeException e) {
            logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
            return;
          }
          logger.info("Greeting: " + response.getMessage());
        }

        /**
         * Greet server. If provided, the first element of {@code args} is the name to use in the
         * greeting.
         */
        public static void main(String[] args) throws Exception {
          // Access a service running on the local machine on port 50051
          CompressingHelloWorldClient client = new CompressingHelloWorldClient("localhost", 50051);
          try {
            String user = "world";
            // Use the arg as the name to greet if provided
            if (args.length > 0) {
              user = args[0];
            }
            client.greet(user);
          } finally {
            client.shutdown();
          }
        }
}
